package book_mianshizhinan.test1_1;

import java.util.Stack;

public class Stack01 {

	Stack<Integer> stackData;
	Stack<Integer> stackMin;

	Stack01() {
		stackData = new Stack<Integer>();
		stackMin = new Stack<Integer>();
	}

	void push(int newNum) {
		if (stackMin.empty()) {
			stackMin.push(newNum);
		} else if (newNum <= getMin()) {
			stackMin.push(newNum);
		}

		this.stackData.push(newNum);

	}

	int pop() {

		if (stackData.empty()) {
			throw new RuntimeException("Your Stack is empty");
		}

		int value = this.stackData.pop();
		if (value == getMin()) {
			stackMin.pop();
		}

		return stackData.pop();

	}

	int getMin() {

		if (stackMin.empty()) {
			throw new RuntimeException("Your Stack is empty!");
		}
		return stackMin.peek();

	}

}
